package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.JwtUtils;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduTeacher;
import com.zjitc.serviceedu.entity.EduUser;
import com.zjitc.serviceedu.entity.vo.RegisterVo;
import com.zjitc.serviceedu.entity.vo.TeacherVo;
import com.zjitc.serviceedu.entity.vo.UserVo;
import com.zjitc.serviceedu.service.EduTeacherService;
import com.zjitc.serviceedu.service.EduUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-10
 */
@RestController
@RequestMapping("/serviceedu/edu-user")
@Api(tags = "用户模块")
public class EduUserController {
    @Autowired
    private EduUserService eduUserService;
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @ApiOperation("登录功能")
    @PostMapping("login")
    public RespBean loginUser(EduUser eduUser){
     String token = eduUserService.login(eduUser);
     return RespBean.success().data("token",token).msg("登录成功");
    }

    @ApiModelProperty("注册功能")
    @PostMapping("Register")
    public RespBean Register(RegisterVo registerVo){
        eduUserService.register(registerVo);
        return RespBean.success().msg("注册成功");
    }

    @GetMapping("getUserInfo")
    public RespBean getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> claims = JwtUtils.getClaimsFromToken(token);
        String nickname = (String) claims.get("nickname");
        String avatar = (String) claims.get("avatar");
        return RespBean.success().data("nickname", nickname).data("avatar", avatar).msg("获取成功");
    }


    @ApiOperation("查询所有用户")
    @GetMapping("userList")
    public RespBean userList(EduUser eduUser) {
        List<EduUser> list = eduUserService.list(null);
        return RespBean.success().data("list",list).msg("查询成功");
    }

    @ApiOperation("添加用户")
    @PostMapping("addUser")
    public RespBean addUser(EduUser eduUser) {
        boolean result = eduUserService.save(eduUser);
        if(!result) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }


    @ApiOperation("修改详情查询")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable("id") Long id) {
        EduUser user = eduUserService.getById(id);
        if(user == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", user).msg("查询成功");
    }

    @ApiOperation("修改用户")
    @PutMapping("updaterUser")
    public RespBean updaterUser(EduUser eduUser) {
        boolean result = eduUserService.updateById(eduUser);
        if(!result) {
            throw new AuthException("修改失败");
        }

        return RespBean.success().msg("修改成功");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("delUser/{id}")
    public RespBean delUser(@PathVariable("id") Long id) {
        boolean result = eduUserService.removeById(id);
        if(!result) {
            throw new AuthException("删除失败");
        }

        return RespBean.success().msg("删除成功");
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("delMoreUser")
    public RespBean delMoreUser(Long[] ids) {
        boolean result = eduUserService.removeByIds(Arrays.asList(ids));
        if(!result) {
            throw new AuthException("批量删除失败");
        }

        return RespBean.success().msg("批量删除成功");
    }

    @ApiOperation("条件查询")
    @GetMapping("getPageBatchs")
    public RespBean getPageBatchs(UserVo userVo) {

        Page<EduUser> page = new Page<>(userVo.getPage(), userVo.getSize());

        QueryWrapper<EduUser> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(userVo.getUsername())) {
            queryWrapper.like("username", userVo.getUsername());
        }
        if (!StringUtils.isEmpty(userVo.getNickname())) {
            queryWrapper.like("nickname", userVo.getNickname().trim());
        }
        if (!StringUtils.isEmpty(userVo.getBeginTime())) {
            queryWrapper.ge("gmt_create", userVo.getBeginTime());
        }
        if (!StringUtils.isEmpty(userVo.getEndTime())) {
            queryWrapper.le("gmt_modified", userVo.getEndTime());  // 使用小写的 endTime
        }
        queryWrapper.orderByAsc("id");
        eduUserService.page(page, queryWrapper);

        if (page == null) {
            throw new AuthException("查询失败");
        }

        List<EduUser> list = page.getRecords();
        Long total = page.getTotal();

        if (list.isEmpty()) {
            return RespBean.success().data("list", list).data("total", total).msg("未找到符合条件的用户");
        }

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");
    }

}

